<?php
declare (strict_types=1);

namespace app\store\controller\student;

use app\store\controller\Controller;
use app\store\model\store\User as StoreUserModel;
use app\store\model\student\User as StudentUserModel;
use think\db\exception\DbException;

class User extends Controller
{
    public function details($apiId)
    {
        $details = StudentUserModel::detail($apiId,['avatar_file', 'id_card_file1', 'id_card_file2']);
        return $this->renderSuccess(compact('details'));
    }

    /**
     * 列表
     * @return array
     * @throws DbException
     */
    public function list()
    {
        $model = new StudentUserModel;
        $param = $this->request->param() ?: [];
        $studentUsermodel = new StoreUserModel;
        $param['channel_id'] = $studentUsermodel->getChildrenUserIds(!empty($param['channel_id']) ? $param['channel_id'] : $this->store['user']['store_user_id']);
        $list = $model->getList($param);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 更新学员
     * @param int $apiId
     * @return array
     */
    public function edit(int $apiId)
    {
        // 管理员详情
        $model = StudentUserModel::detail($apiId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 新增学员
     * @return array
     */
    public function add()
    {
        // 管理员详情
        $model = new StudentUserModel;
        // 更新记录
        if ($student_user_id = $model->add($this->postForm())) {
            return $this->renderSuccess(['student_user_id' => $student_user_id], '更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除学员
     * @param int $apiId
     * @return array|bool
     */
    public function delete(int $apiId)
    {
        // 管理员详情
        $model = StudentUserModel::detail($apiId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}
